package com.jingogo.controller;

import com.jingogo.common.Result;
import com.jingogo.entity.Address;
import com.jingogo.service.AddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/address")
@Tag(name = "地址管理", description = "用户地址管理接口")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @PostMapping("/add")
    @Operation(summary = "添加地址", description = "添加用户地址")
    @PreAuthorize("hasRole('USER')")
    public Result<String> addAddress(@RequestBody Address address) {
        addressService.addAddress(address);
        return Result.success("地址添加成功");
    }

    @PutMapping("/edit")
    @Operation(summary = "编辑地址", description = "编辑用户地址")
    @PreAuthorize("hasRole('USER')")
    public Result<String> editAddress(@RequestBody Address address) {
        addressService.editAddress(address);
        return Result.success("地址编辑成功");
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除地址", description = "删除用户地址")
    @PreAuthorize("hasRole('USER')")
    public Result<String> deleteAddress(@RequestParam Long addressId) {
        addressService.deleteAddress(addressId);
        return Result.success("地址删除成功");
    }

    @GetMapping("/list")
    @Operation(summary = "地址列表", description = "获取用户地址列表")
    @PreAuthorize("hasRole('USER')")
    public Result<List<Address>> listAddress(@RequestParam Long userId) {
        return Result.success(addressService.listAddress(userId));
    }
} 